package com.zd.shopping.activity;

import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zd.shopping.R;
import com.zd.shopping.apis.UserLoginApi;
import com.zd.shopping.utils.RouterUtils;
import com.zwz.mall.api.OnRequestListener;
import com.zwz.mall.base.BaseActivity;
import com.zwz.mall.managers.UserManager;
import com.zwz.mall.models.User;
import com.zwz.mall.utils.BarHeightUtils;
import com.zwz.mall.utils.LoadingDialog;
import com.zwz.mall.utils.ToastUtils;

import butterknife.BindView;

/**
 *
 * @author Administrator
 * @date 2019/10/30 0030
 */

public class LoginActivity extends BaseActivity
{
    @BindView(R.id.et_inputpassword)
    EditText mEtinputpassword;
    @BindView(R.id.et_login_number)
    EditText mEtnumber;
    @BindView(R.id.tv_login)
    TextView mTvlogin;
    @BindView(R.id.fake_status_bar)
    View fakeStatusBar;
    private UserLoginApi userLoginApi = new UserLoginApi ();
    private String phonenumber;

    @Override
    public int getLayoutId()
    {
        return R.layout.activity_login;
    }

    @Override
    public void initPresenter()
    {

    }

    @Override
    public void initView()
    {
        BarHeightUtils.setStatusBarHeight(fakeStatusBar, getStatusBarHeight());
        mEtnumber.setInputType(InputType.TYPE_CLASS_TEXT );
        mEtinputpassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }

    @Override
    public void fillData()
    {

    }

    @Override
    public void setListener()
    {
        mEtinputpassword.addTextChangedListener(new TextWatcher () {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //EditText输入状态改变，Button背景颜色也改变
                if ("".equals(mEtinputpassword.getText().toString().trim())) {
                    mTvlogin.setEnabled(false);
                } else {
                    mTvlogin.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mTvlogin.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                login();
            }
        });
    }
    private void login()
    {
//        RouterUtils.openMianActivity(LoginActivity.this);
        phonenumber = mEtnumber.getText().toString();
        String password = mEtinputpassword  .getText().toString();
        userLoginApi.password=password;
        userLoginApi.username= phonenumber;
        if (!isSuccess(phonenumber, password)) {
            return;
        }
        LoadingDialog.showDialogForLoading(mActivity, "登入中...");
        userLoginApi.loadData(new OnRequestListener ()
        {
            @Override
            public void onBefore()
            {

            }

            @Override
            public void onSuccess()
            {
                User user = new User ();
                user.setUsername (phonenumber);
                UserManager.getInstance ().setUser (user);
                ToastUtils.showShort("登录成功");
                RouterUtils.openMianActivity(LoginActivity.this);
                LoadingDialog.cancelDialogForLoading();
            }

            @Override
            public void onFailure(Throwable error)
            {
                LoadingDialog.cancelDialogForLoading();
                Log.e("登录失败","登录失败");
            }

            @Override
            public void onNetWorkError()
            {

            }
        });
    }
    private boolean isSuccess(String number, String firstPsw) {

        if (TextUtils.isEmpty(number)) {
            ToastUtils.showShort("请输入账号");
            return false;
        }
        if (TextUtils.isEmpty(firstPsw)) {
            ToastUtils.showShort("请输入密码");
            return false;
        }
        return true;
    }
}
